package com.signway.javaui.slice.example;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.base.BaseAbilitySlice;
import com.signway.javaui.utils.SwLog;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Slider;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class CatImageSlice extends BaseAbilitySlice {
    float downX;
    float downY;
    float imgX;
    float imgY;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_example_cat_image);
        initView();
    }

    private void initView() {
        Image image = (Image) findComponentById(ResourceTable.Id_image);
        image.setTouchEventListener((component, touchEvent) -> {
            MmiPoint point = touchEvent.getPointerPosition(0);
            switch (touchEvent.getAction()) {
                case TouchEvent.PRIMARY_POINT_DOWN: {
                    downX = point.getX();
                    downY = point.getY();
                    imgX = image.getTranslationX();
                    imgY = image.getTranslationY();
                } break;
                case TouchEvent.POINT_MOVE: {
                    float moveX = point.getX() - downX;
                    float moveY = point.getY() - downY;
                    SwLog.info("[" + moveX + ", " + moveY + "]");
                    image.setTranslation(imgX + moveX, imgY + moveY);
                } break;
            }
            return true;
        });

        Slider slider = (Slider) findComponentById(ResourceTable.Id_slider);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                image.setScale((float) (i * 0.1), (float) (i * 0.1));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
    }
}
